package com.hzit.boot.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.boot.entity.Classes;
import com.hzit.boot.entity.PageResult;
import com.hzit.boot.entity.Student;
import com.hzit.boot.mapper.ClassesMapper;
import com.hzit.boot.mapper.StudentMapper;
import com.hzit.boot.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;
	@Autowired
	private StringRedisTemplate redisTemplate;   //开发中常用
	//1. 查询学生
	@Override
	public List<Student> findAll() {
		//1.1 查询所有学生
		List<Student> students = studentMapper.selectAll();
		//1.2 遍历学生为学生指定班级名称
		for (Student student : students) {
			Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
			student.setCname(classes.getCname());
		}
		return students;
	}

	//2. 分页查询
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 查询所有学生
		List<Student> students = findAll();
		//2.3 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;

		//2.4 返回PageResult对象
		return new PageResult<>(studentPage.getTotal(),studentPage.getPages(),studentPage.getResult(),page);
	}

	//3. 查询带分页功能
	@Override
	public PageResult<Student> search(int page, int pageSize, Student student) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 查询所有学生
		//3.2.1 定义查询实例
		Example example = Example.builder(Student.class).build();
		//3.2.2 得到实例的查询条件对象
		Example.Criteria criteria = example.createCriteria();
		if(student != null){
			if(StringUtils.isNotBlank(student.getSname())){
				criteria.andLike("sname","%" + student.getSname() + "%");
			}
			if(StringUtils.isNotBlank(student.getAddr())){
				criteria.andLike("addr","%" + student.getAddr() + "%");
			}
			if(student.getCid() != null && student.getCid() != 0){
				criteria.andEqualTo("cid",student.getCid());
			}
		}
		//3.3 实例查询学生
		List<Student> students = studentMapper.selectByExample(example);
		for (Student st : students) {
			Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
			st.setCname(classes.getCname());
		}
		//3.4 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;

		//3.5 返回PageResult对象
		return new PageResult<>(studentPage.getTotal(),studentPage.getPages(),studentPage.getResult(),page);
	}

	//4. 在redis中查询学生列表
	@Override
	public List<Student> findAll2() {
		List<Student> students = null;
		//4.1 从redis中取得学生列表的字符串
		String studs = redisTemplate.opsForValue().get("studentList");
		//4.2 将字符串转换为集合
		if(StringUtils.isNotBlank(studs)){
			students = JSON.parseArray(studs, Student.class);
		}else{  //4.3 如果为null就从数据库中取
			students = studentMapper.selectAll();
			//4.4 放数据到redis中
			//参数1：key的名字
			//参数2：存放的字符串数据
			//参数3：到期时间
			//参数4：到期时间的单位
			redisTemplate.opsForValue().set("studentList",JSON.toJSONString(students),10, TimeUnit.SECONDS);
			System.out.println("正在从数据库中取得studList数据。。。。");
		}
		return students;
	}

	//5. 添加学生
	@Override
	public void add(Student stud) {
		studentMapper.insert(stud);
	}

	//6. 修改学生
	@Override
	public void update(Student stud) {
		studentMapper.updateByPrimaryKey(stud);
	}

	//7. 根据学号删除学生
	@Override
	public void delete(String sid) {
		studentMapper.deleteByPrimaryKey(sid);
	}
}
